//
//  CourseThreeViewTableViewCell.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/26.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "CourseThreeViewTableViewCell.h"
#import "CourseThreeViewCollectionViewCell.h"
@implementation CourseThreeViewTableViewCell

-(NSMutableArray *)imageArr
{
    if (!_imageArr) {
        _imageArr = [NSMutableArray array];
    }
    return _imageArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

/*
 @property(nonatomic,strong)UILabel *titlel;
 @property(nonatomic,strong)UILabel *timeL;
 @property(nonatomic,strong)UILabel *contents;
 @property(nonatomic,strong)UICollectionView *collectionV;
 @property(nonatomic,strong)UIView *lineV;
 */

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.titlel = [[UILabel alloc]init];
        self.timeL = [[UILabel alloc]init];
        self.contents = [[UILabel alloc]init];
        self.lineV = [[UIView alloc]init];

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((KscreenW-20-10)/2, (KscreenW-20-10)/2);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

        self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        self.collectionV.delegate = self;
        self.collectionV.dataSource = self;
        [self.collectionV registerNib:[UINib nibWithNibName:@"CourseThreeViewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CourseThreeViewCollectionViewCell"];

        [self.contentView addSubview:self.titlel];
        [self.contentView addSubview:self.timeL];
        [self.contentView addSubview:self.contents];
        [self.contentView addSubview:self.lineV];
        [self.contentView addSubview:self.collectionV];



        self.titlel.textColor = UIColors(0x333333);
        self.timeL.textColor = UIColors(0x444444);
        self.lineV.backgroundColor = UIColors(0xEFEFEF);
        self.contents.numberOfLines = 0;
        self.contents.textColor = UIColors(0x555555);

        self.titlel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightThin];
        self.timeL.font = [UIFont systemFontOfSize:17 weight:UIFontWeightThin];
        self.contents.font = [UIFont systemFontOfSize:17 weight:UIFontWeightThin];
        self.collectionV.backgroundColor = [UIColor whiteColor];

    }
    return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CourseThreeViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CourseThreeViewCollectionViewCell" forIndexPath:indexPath];

    NSLog(@"%@",self.imageArr);

    NSString *picUrl = self.imageArr[indexPath.row];

    NSString *bigPic = [picUrl stringByReplacingOccurrencesOfString:@"/thumb" withString:@""];

    [cell.titleImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,bigPic]] placeholderImage:Kplaceholderimage];

    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.imageCount = self.imageArr.count;
    browser.currentImageIndex = indexPath.row;
    browser.sourceImagesContainerView = self.collectionV;
    browser.delegate = self;
    [browser show];
}


- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSURL *url = [NSURL new];
    NSMutableArray *thumbUrlArr = [NSMutableArray array];
    for (NSString *url in self.imageArr)
    {
        [thumbUrlArr addObject:[url stringByReplacingOccurrencesOfString:@"/thumb" withString:@""]];
    }
    if (index < thumbUrlArr.count) {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,thumbUrlArr]];
    }
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    CourseThreeViewCollectionViewCell *Cell = (CourseThreeViewCollectionViewCell *)[self.collectionV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    UIImageView *imageView = Cell.titleImageV;
    return imageView.image;
}


/*@property(nonatomic,strong)UILabel *titlel;
 @property(nonatomic,strong)UILabel *timeL;
 @property(nonatomic,strong)UILabel *contents;
 @property(nonatomic,strong)UICollectionView *collectionV;
 @property(nonatomic,strong)UIView *lineV;*/
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.titlel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.top.equalTo(self.contentView.mas_top).offset(10);
    }];
    [self.timeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.centerY.equalTo(self.titlel.mas_centerY);
    }];
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeL.mas_bottom).offset(8);
        make.left.equalTo(self.contentView.mas_left).offset(5);
        make.right.equalTo(self.contentView.mas_right).offset(-5);
        make.height.mas_equalTo(1);
    }];
    [self.contents mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.top.equalTo(self.lineV.mas_bottom).offset(8);
    }];
    [self.collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.top.equalTo(self.contents.mas_bottom).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-5);
    }];
}

@end
